package cn.com.cetccst.mdm.engine;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.widget.Toast;

import androidx.core.content.FileProvider;

import com.google.gson.Gson;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;

public class MockMDMEngine extends BaseMDMEngine {
    private MockData dummyData = new MockData();

    public MockMDMEngine(Context context) {
        super(context);
        loadData();
    }

    @Override
    public void controlBluetooth(boolean disabled) {
        logger.log("set blue tooth " + disabled);
        dummyData.isBluetoothDisabled = disabled;
        persistData();
    }

    @Override
    public boolean isBluetoothControlled() {
        boolean ret = dummyData.isBluetoothDisabled;
        logger.log("check blue tooth " + ret);
        return ret;
    }

    @Override
    public void controlCamera(boolean disabled) {
        logger.log("set Camera " + disabled);
        dummyData.isCameraDisabled = disabled;
        persistData();
    }

    @Override
    public boolean isCameraControlled() {
        boolean ret = dummyData.isCameraDisabled;
        logger.log("check Camera " + ret);
        return ret;
    }

    @Override
    public void controlWifi(boolean disabled) {
        logger.log("set wifi " + disabled);
        dummyData.isWifiDisabled = disabled;
        persistData();
    }

    @Override
    public boolean isWifiControlled() {
        boolean ret = dummyData.isWifiDisabled;
        logger.log("check wifi " + ret);
        return ret;
    }

    @Override
    public void controlMicroPhone(boolean disabled) {
        logger.log("set MicroPhone " + disabled);
        dummyData.isMicroPhoneDisabled = disabled;
        persistData();
    }

    @Override
    public boolean isMicroPhoneControlled() {
        boolean ret = dummyData.isMicroPhoneDisabled;
        logger.log("check MicroPhone " + ret);
        return ret;
    }

    @Override
    public void controlExternalStorage(boolean disabled) {
        logger.log("set ExternalStorage " + disabled);
        dummyData.isExternalStorageDisabled = disabled;
        persistData();
    }

    @Override
    public boolean isExternalStorageControlled() {
        boolean ret = dummyData.isExternalStorageDisabled;
        logger.log("check ExternalStorage " + ret);
        return ret;
    }

    @Override
    public void controlScreenShot(boolean disabled) {
        logger.log("set ScreenShot " + disabled);
        dummyData.isScreenShotDisabled = disabled;
        persistData();
    }

    @Override
    public boolean isScreenShotControlled() {
        boolean ret = dummyData.isScreenShotDisabled;
        logger.log("check ScreenShot " + ret);
        return ret;
    }

    @Override
    public void controlNFC(boolean disabled) {
        logger.log("set NFC " + disabled);
        dummyData.isNFCDisabled = disabled;
        persistData();
    }

    @Override
    public boolean isNFCControlled() {
        boolean ret = dummyData.isNFCDisabled;
        logger.log("check NFC " + ret);
        return ret;
    }

    @Override
    public void controlMobileHotSpot(boolean disabled) {
        logger.log("set MobileHotSpot " + disabled);
        dummyData.isMobileHotSpotDisabled = disabled;
        persistData();
    }

    @Override
    public boolean isMobileHotSpotControlled() {
        boolean ret = dummyData.isMobileHotSpotDisabled;
        logger.log("check MobileHotSpot " + ret);
        return ret;
    }

    @Override
    public void controlCallout(boolean disabled) {
        logger.log("set Callout " + disabled);
        dummyData.isCalloutDisabled = disabled;
        persistData();
    }

    @Override
    public boolean isCalloutControlled() {
        boolean ret = dummyData.isCalloutDisabled;
        logger.log("check Callout " + ret);
        return ret;
    }

    @Override
    public void controlSms(boolean disabled) {
        logger.log("set Sms " + disabled);
        dummyData.isSmsDisabled = disabled;
        persistData();
    }

    @Override
    public boolean isSmsControlled() {
        boolean ret = dummyData.isSmsDisabled;
        logger.log("check Sms " + ret);
        return ret;
    }

    @Override
    public void controlMobileData(boolean disabled) {
        logger.log("set MobileData " + disabled);
        dummyData.isMobileDataDisabled = disabled;
        persistData();
    }

    @Override
    public boolean isMobileDataControlled() {
        boolean ret = dummyData.isMobileDataDisabled;
        logger.log("check MobileData " + ret);
        return ret;
    }

    @Override
    public void controlVolumeDown(boolean disabled) {
        logger.log("set Volume " + disabled);
        dummyData.isVolumeDisabled = disabled;
        persistData();
    }

    @Override
    public boolean isVolumeDownControlled() {
        boolean ret = dummyData.isVolumeDisabled;
        logger.log("check Volume " + ret);
        return ret;
    }

    @Override
    public void controlAutoTime(boolean disabled) {
        logger.log("set AutoTime " + disabled);
        dummyData.isAutoTimeDisabled = disabled;
        persistData();
    }

    @Override
    public boolean isAutoTimeControlled() {
        boolean ret = dummyData.isAutoTimeDisabled;
        logger.log("check AutoTime " + ret);
        return ret;
    }

    @Override
    public void controlUsbData(boolean disabled) {
        logger.log("set UsbData " + disabled);
        dummyData.isUsbDataDisabled = disabled;
        persistData();
    }

    @Override
    public boolean isUsbDataControlled() {
        boolean ret = dummyData.isUsbDataDisabled;
        logger.log("check UsbData " + ret);
        return ret;
    }

    @Override
    public void controlUsbDebug(boolean disabled) {
        logger.log("set Adb " + disabled);
        dummyData.isAdbDisabled = disabled;
        persistData();
    }

    @Override
    public boolean isUsbDebugControlled() {
        boolean ret = dummyData.isAdbDisabled;
        logger.log("check Adb " + ret);
        return ret;
    }

    @Override
    public void controlRestoreFactory(boolean disabled) {
        logger.log("set RestoreFactory " + disabled);
        dummyData.isRestoreFactoryDisabled = disabled;
        persistData();
    }

    @Override
    public boolean isRestoreFactoryControlled() {
        boolean ret = dummyData.isRestoreFactoryDisabled;
        logger.log("check RestoreFactory " + ret);
        return ret;
    }

    @Override
    public void controlSystemUpgrade(boolean disabled) {
        logger.log("set SystemUpgrade " + disabled);
        dummyData.isSystemUpgradeDisabled = disabled;
        persistData();
    }

    @Override
    public boolean isSystemUpgradeControlled() {
        boolean ret = dummyData.isSystemUpgradeDisabled;
        logger.log("check SystemUpgrade " + ret);
        return ret;
    }

    @Override
    public void controlGPS(boolean disabled) {

    }

    @Override
    public boolean setDisallowRunningApps(List<String> packageNames, boolean isBlock) {
        logger.log("setDisallowRunningApps " + packageNames);
        dummyData.blackListRunningApps = packageNames;
        persistData();
        return true;
    }

    @Override
    public List<String> getDisallowRunningApps() {
        logger.log("getDisallowRunningApps");
        return dummyData.blackListRunningApps;
    }

    @Override
    public void installPackage(String path) {
        File apkFile = new File(path);
        Uri uri;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            uri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", apkFile);
            context.grantUriPermission(context.getPackageName(), uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
        } else {
            uri = Uri.fromFile(apkFile);
        }
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.setDataAndType(uri, "application/vnd.android.package-archive");
        context.startActivity(intent);
    }

    @Override
    public void uninstallPackage(String packageName) {
        logger.log("uninstallPackage " + packageName);
        Uri packageURI = Uri.parse("package:" + packageName);
        Intent uninstallIntent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE, packageURI);
        uninstallIntent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
        uninstallIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(uninstallIntent);
    }

    @Override
    public void formatSDCard() {
        logger.log("formatSDCard");
        Toast.makeText(context, "formatSDCard", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void factoryReset() {
        logger.log("factoryReset");
        Toast.makeText(context, "factoryReset", Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean setInstallSourceApps(List<String> packageNames) {
        logger.log("setInstallSourceApps " + packageNames);
        dummyData.installSourceApps = packageNames;
        persistData();
        return true;
    }

    @Override
    public List<String> getInstallSourceApps() {
        logger.log("getInstallSourceApps");
        return dummyData.installSourceApps;
    }

    @Override
    public boolean setNetworkWhiteListApps(List<String> packageNames) {
        logger.log("setNetworkWhiteListApps " + packageNames);
        dummyData.whiteListNetworkApps = packageNames;
        persistData();
        return true;
    }

    @Override
    public List<String> getNetworkWhiteListApps() {
        logger.log("getNetworkWhiteListApps");
        return dummyData.whiteListNetworkApps;
    }

    @Override
    public boolean setKeepLiveApps(List<String> packageNames) {
        logger.log("setKeepLiveApps " + packageNames);
        dummyData.keepAliveApps = packageNames;
        persistData();
        return true;
    }

    @Override
    public List<String> getKeepLiveApps() {
        logger.log("getKeepLiveApps");
        return dummyData.keepAliveApps;
    }

    @Override
    public boolean setSuperApps(List<String> packageNames) {
        logger.log("setSuperApps " + packageNames);
        dummyData.superApps = packageNames;
        persistData();
        return true;
    }

    @Override
    public List<String> getSuperApps() {
        logger.log("getSuperApps");
        return dummyData.superApps;
    }

    @Override
    public boolean setDevicePassword(String password) {
        Toast.makeText(context, "setDevicePassword " + password, Toast.LENGTH_SHORT).show();
        return true;
    }

    private void persistData() {
        String data = new Gson().toJson(dummyData);
        try (FileOutputStream fileOutputStream = new FileOutputStream(context.getFilesDir() + "/mdm.dat")) {
            fileOutputStream.write(data.getBytes());
            fileOutputStream.flush();
        } catch (Exception e) {
        }
    }

    private void loadData() {
        try (FileInputStream fileInputStream = new FileInputStream(context.getFilesDir() + "/mdm.dat")) {
            byte[] data = new byte[fileInputStream.available()];
            fileInputStream.read(data);
            String json = new String(data, "utf-8");
            dummyData = new Gson().fromJson(json, MockData.class);
        } catch (Exception e) {
        }
    }

    @Override
    public String getName() {
        return "Mock";
    }
}
